<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/12
 * Time: 16:14
 */
namespace app\index\controller;
use think\Db;
use think\Request;

class Postbusiness extends Common
{
	//判断是否登录
	public function _initialize()
	{
		parent::_initialize();
	}
	/**
	 * 查看后付款给商家
	 * @param string get.successively  付款方式
	 */
	public function index()
	{
		$db = Db::table('business')->order('id desc')->where('status',2)->where('successively',2);
		$where = ['query'=>[]];
		if(!empty(trim(input('get.title')))){
			$title = trim(input('get.title'));
			$db->where('name|linkman|telephone','like',"%{$title}%");
			$where['query']['title'] = $title;
		}
		$result = $db->order('id desc')->paginate(8,false,$where);
		return view('index',compact('result','successively','title'));
	}
	
	/**
	 * 押金证明
	 * @param string get.id 商家id
	 */
	public function prove()
	{
		$id = input('get.id');
		return view('prove',compact('id'));
	}
	
	/**
	 * 上传押金证明
	 * @param string post.id 商家id
	 */
	public function uploadprove()
	{
		if(request()->isAjax()){
			try{
				$id = input('post.bid');
				if(request()->file('file')){
					$image = request()->file('file');
					$provePath = ROOT_PATH.DS.'public'.DS.'business'.DS.$id.'prove';
					$prove = $image->validate(['ext'=>'gif,png,jpg'])->move($provePath);
					if($prove){
						$input['prove'] = 'business'.DS.$id.DS.'prove'.DS.$prove->getSaveName();
						$result = Db::table('business')->where('id',$id)->update($input);
						if(!$result){
							throw new \PDOException('上传失败');
						}
					}else{
						throw new \PDOException($image->getError());
					}
				}
				$success = ['success'=>'上传成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	
	/**
	 * 商品删除
	 * @param string post.id 商家id
	*/
	public function delete()
	{
		if(request()->isAjax()){
			try{
				$id = input('post.id');
				$result = Db::table('business')->where('id',$id)->update(['status'=>4]);
				if(!$result){
					throw new \PDOException('删除失败');
				}
				$success = ['success'=>'删除成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	
	/**
	 * 商家商品
	 * @param string get.id 商家id
	*/
	public function goods()
	{
		$bid = input('get.id');
		$where = ['query'=>[]];
		$db = Db::table('product')->whereNotIn('status',[2,4,5])->where('bid',$bid)->order('id desc');
		if(!empty(trim(input('get.title')))){
			$title = trim(input('get.title'));
			$db->where('pname|purchaseprice|retailprice|bid','like',"%{$title}%");
			$where['query']['title'] = $title;
		}
		$where['query']['id'] = $bid;
		$result = $db->paginate(8,false,$where);
		return view('goods',compact('result','title','bid'));
		
	}
	
	/**
	 * 删除商家商品
	 * @param string post.id 商品id
	*/
	public function goodsdel()
	{
		if(request()->isAjax()){
			try{
				$id = input('post.id');
				$result = Db::table('product')->where('id',$id)->update(['status'=>2]);
				if(!$result){
					throw new \PDOException('删除失败');
				}
				$succcess = ['success'=>'删除成功'];
				return json($succcess);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	
	/**
	 * 商家订单
	 * @param string get.id 商家订单
	*/
	public function order()
	{
		$where = ['query'=>[]];
		$bid = input('get.bid');
		$db = Db::table('postpayment')->where('bid',$bid)->order('id desc')->group('ordernumber');
		if(!empty(trim(input('get.ordernumbe')))){
			$ordernumber = trim(input('get.ordernumbe'));
			$db->where('ordernumber',$ordernumber);
			$where['query']['ordernumber'] = $ordernumber;
		}
		$result = $db->paginate(8,false,$where);
		return view('order',compact('result','ordernumber','bid'));
	}
	
	/**
	 * 详情
	 * @param string get.ordernumber 订单号
	*/
	public function detail()
	{
		$result = Db::table('postpayment')
			->field('postpayment.id,postpayment.number,postpayment.total,postpayment.actualtotal,
						product.pname,product.purchaseprice,product.retailprice,postpayment.ordernumber,postpayment.date')
			->join('business','business.id = postpayment.id','LEFT')
			->join('shop','shop.id = postpayment.sid','LEFT')
			->join('product','product.id = postpayment.pid','LEFT')
			->where('postpayment.ordernumber',input('get.ordernumber'))
			->where('postpayment.status','<>',0)
			->paginate(8);
		
		return view('detail',compact('result'));
	}
	
	/**
	 * 删除订单
	 * @param string post.id 订单id
	*/
	public function orderdel()
	{
		if(request()->isAjax()){
			try{
				$id = input('post.id');
				$result = Db::table('postpayment')->where('id',$id)->update(['status'=>0]);
				if(!$result){
					throw new \PDOException('删除失败');
				}
				$success = ['success'=>'删除成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			
			}
		}
	}
	
	/**
	 * 订单详情
	 * @param string get.id 订单id
	*/
	public function orderdetail()
	{
		$id = input('get.id');
		$result = Db::table('postpayment')->join('shop','shop.id = postpayment.sid','LEFT')
				->join('business','business.id = postpayment.bid','LEFT')
				->join('product','product.id = postpayment.pid','LEFT')
				->where('postpayment.id',$id)
				->find();
		$result['image'] = json_decode($result['image']);
		return view('orderdetail',compact('result'));
	}
	
	/**
	 * 店铺id
	 * @param string get.id 店铺id
	*/
	public function shop()
	{
		$bid = input('get.id');
		$result = Db::table('postpayment')->field('shop.linkman,shop.telephone,shop.address,shop.id')
			->join('shop','shop.id = postpayment.sid','LEFT')
			->join('business','business.id = postpayment.bid','RIGHT')
			->where('postpayment.bid',$bid)
			->group('shop.address')
			->paginate(8);
		return view('shop',compact('result','bid'));
	}
	
	/**
	 * 查看商品
	 * @param string get.sid 店铺id
	 * @param string get.bid 商家id
	*/
	public function product()
	{
		$sid = input('get.sid');
		$bid = input('get.bid');
		$where = ['query'=>[]];
		$where['query']['sid'] = $sid;
		$where['query']['bid'] = $bid;
		$db = Db::table('inventory')
			->field('product.pname,product.id as pid,inventory.pid')
			->join('product','product.id = inventory.pid','LEFT')
			->join('apply','apply.postid = inventory.postid','LEFT')
			->whereNotNull('inventory.postid')
			->where('inventory.bid',$bid)
			->where('inventory.sid',$sid)
			->group('product.pname')
			->order('inventory.id desc');
		if(!empty(trim(input('get.title')))){
			$title = trim(input('get.title'));
			$db->where('product.pname',$title);
			$where['query']['title'] = $title;
		}
		$result = $db->paginate(8,false,$where);
		return view('product',compact('result','sid','bid','title'));
	}
	
	/**
	 * 申请详情
	 * @param string get.pid 商品id
	 * @param string get.sid 店铺id
	 * @param string get.bid 商家id
	*/
	public function applydetail()
	{
		$pid = input('get.pid');
		$sid = input('get.sid');
		$bid = input('get.bid');
		$count = Db::table('apply')->where('pid',$pid)->where('sid',$sid)->where('bid',$bid)->where('status',2)->count();
		if($count>0){
			$result = Db::table('inventory')
				->field('product.pname,inventory.sellnumber,inventory.number,
							sum(apply.number) as anumber,inventory.id')
				->join('product','product.id = inventory.pid','LEFT')
				->join('apply','apply.postid = inventory.postid','RIGHT')
				->where('apply.pid',$pid)
				->where('apply.sid',$sid)
				->where('apply.bid',$bid)
				->where('apply.status',2)
				->group('product.pname')
				//->order('apply.id')
				->select();
		}else{
			$result = Db::table('inventory')
				->field('product.pname,inventory.sellnumber,inventory.number,inventory.id')
				->join('product',' product.id = inventory.pid ','LEFT')
				//->join('apply',' inventory.postid = apply.postid ','RIGHT')
				->where('inventory.pid',$pid)
				->where('inventory.sid',$sid)
				->where('inventory.bid',$bid)
				//->where('apply.status',1)
				->group('product.pname')
				//->order('apply.id')
				->select();
		}
		return view('applydetail',compact('result','sid','pid','bid'));
	}
	/**
	 * 结算
	 * @param string get.inventoryid 库存id
	 * @param string get.sellnumber 应结算的数量
	 * @param string get.sid 店铺id
	 * @param string get.bid 商家id
	 * @param string get.pid 商品id
	*/
	public function settlement()
	{
		$sid = input('get.sid');
		$bid = input('get.bid');
		$pid = input('get.pid');
		$inventoryid = input('get.inventoryid');
		$sellnumber = input('get.sellnumber');
		$result = Db::table('inventory')
			->field('product.pname,product.purchaseprice')
			->join('product','product.id = inventory.pid','LEFT')
			->where('inventory.id',$inventoryid)
			->find();
		//计算总价
		$total = $sellnumber * $result['purchaseprice'];
		return view('settlement',compact('result','sellnumber','sid','bid','pid','total','inventoryid'));
	}
	
	/**
	 * 上传押金证明
	 * @param array Request()->file('文件名称')
	*/
	public function uploadimage()
	{
		if(request()->isAjax()){
			try{
				if(request()->file('uploadimage')){
					$pid = input('get.pid');
					$prove = request()->file('uploadimage');
					$provePath = ROOT_PATH.DS.'public'.DS.'apply'.DS.$pid;
					$info = $prove->move($provePath);
					if($info){
						$savepath = 'apply'.DS.$pid.DS.$info->getSaveName();
						$success = ['image'=>$savepath];
						return json($success);
					}else{
						throw new \PDOException($prove->getError());
					}
				}
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	/**
	 * 执行结算
	 * @param string post.sid 店铺id
	 * @param string post.bid 商家id
	 * @param string post.pid 商家id
	 * @param string post.pid 商家id
	 * @param string post.unitprice 单价
	 * @param string post.number 结算的数量
	 * @param string post.total 总价
	 * @param string post.uploadprove 支付凭证
	 */
	public function dosettlement()
	{
		try{
			Db::startTrans();
			$input['sid'] = input('post.sid');
			$input['bid'] = input('post.bid');
			$input['pid'] = input('post.pid');
			//$input['pname'] = input('post.pname');
			$input['unitprice'] = input('post.unitprice');
			$input['sellnumber'] = input('post.number');
			$input['total'] = input('post.total');
			$input['prove'] = input('post.uploadprove');
			$input['ordernumber'] = time();
			$input['date'] = date("Y-n-d H:i:s",time());
			$result = Db::table('settlement')->insertGetId($input);
			if($result){
				$bool = Db::table('apply')->where('sid',$input['sid'])->where('bid',$input['bid'])
					->where('pid',$input['pid'])->where('status',1)->update(['status'=>2]);
				if($bool){
					Db::commit();
					$success = ['success'=>'结算成功'];
					return json($success);
				}else{
					throw new \PDOException('结算失败');
				}
			}else{
				throw new \PDOException('结算失败');
			}
		}catch (\PDOException $e){
			Db::rollback();
			$error = ['error'=>$e->getMessage()];
			return json($error);
		}
	}
	
	/**
	 * 结算记录
	 * @param string  get.id 商家id
	 * @param string  get.title 要检索的字段
	*/
	public function recordsettlement()
	{
		$id = input('get.id');
		$where = ['query'=>[]];
		$db = Db::table('settlement')
			->field('settlement.ordernumber,settlement.pid,settlement.date,settlement.total')
			->join('product','product.id = settlement.pid','LEFT')
			->join('business','business.id = settlement.bid','RIGHT')
			->group('settlement.ordernumber')
			->order('settlement.ordernumber desc')
			->where('business.id',$id);
		if(!empty(trim(input('get.title')))){
			$title = trim(input('get.title'));
			$db->where('product.pname',$title);
			$where['query']['title'] = $title;
		}
		$where['query']['id'] = $id;
		$result = $db->paginate(8,false,$where);
		return view('recordsettlement',compact('result','id'));
	}
	
	/**
	 * 结算记录详情
	 * @param string get.ordernumber 订单号
	*/
	public function recorddetail()
	{
		$ordernumber = input('get.ordernumber');
		$where['query']['ordernumber'] = $ordernumber;
		$result = Db::table('settlement')
			->field('product.pname,settlement.unitprice,settlement.sellnumber,settlement.total,
					settlement.date,settlement.ordernumber,settlement.prove,settlement.id')
			->join('product','product.id = settlement.pid','LEFT')
			->join('business','business.id = settlement.bid','LEFT')
			->where('settlement.ordernumber',$ordernumber)
			->where('settlement.status',1)
			->order('settlement.id desc')
			->paginate(8,false,$where);
		return view('recorddetail',compact('result','ordernumber'));
	}
}